import { createRouter, createWebHistory } from 'vue-router';
import Home from '@/views/home/index.vue';
import detection from '@/views/detection/index/index.vue';
import equip from '@/views/detection/equip/index.vue';


import layout from '@/views/layout/index.vue';



import operation from '@/views/operation/index/index.vue';

import statistics from '@/views/statistics/index/index.vue';



import menu from '@/views/system/menu/index.vue';


import login from '@/views/login/index.vue';



const routes = [
  {
    path: '/login',
    name: 'login',
    title: '登录',

    component: login,
  },
  {
    path: '/',
    name: 'Home',
    title: '首页',
    component: Home,
    meta: { requiresAuth: false },
    
  },
  {
    path: '/detection',
    name: 'detection',
    title: '运营平台',
    component: detection,
    meta: { requiresAuth: false },
    children: [
      {
        path: '/detection/yunyng',
        name: 'yunyng',
        title: "维护",
        icon: 'icon-shouye3',
        component: layout,
        // redirect: '/detection/yunyng/dayIndex',
        isTre: true,
        meta: { requiresAuth: false },
        children: [
          {
            path: '/detection/yunyng/day',
            name: 'day',
            title: "日报提交",
            icon: 'icon-shouye3',
            component: layout,
            redirect: '/detection/yunyng/day/dayIndex',
            isTre: true,
            meta: { requiresAuth: false},
            children: [
              {
                path: 'dayIndex',
                name: 'dayIndex',
                title: "日报提交",
                meta: { requiresAuth: false}, 
                component: () => import('@/views/detection/equip/record_day/index.vue'),
              } 
            ]
          },  
        ]
      },{
        path: '/detection/upkeep',
        name: 'upkeep',
        title: "维护",
        icon: 'icon-shouye3',
        component: layout,
        isTre: true,
        meta: { requiresAuth: false },
        children: [
          // {
          //   path: '/detection/upkeep/day',
          //   name: 'day',
          //   title: "日报提交",
          //   icon: 'icon-shouye3',
          //   component: layout,
          //   redirect: '/detection/upkeep/day/dayIndex',
          //   isTre: true,
          //   meta: { requiresAuth: false},
          //   children: [
          //     {
          //       path: 'dayIndex',
          //       name: 'dayIndex',
          //       title: "设备管理",
          //       meta: { requiresAuth: false}, 
          //       component: () => import('@/views/detection/site/index.vue'),
          //     } 
          //   ]
          // },
          {
            path: '/detection/upkeep/equip',
            name: 'equip',
            title: "设备管理",
            icon: 'icon-shouye3',
            component: layout,
            redirect: '/detection/upkeep/equip/equipIndex',
            isTre: true,
            meta: { requiresAuth: false},
            children: [
              {
                path: 'equipIndex',
                name: 'equipIndex',
                title: "设备管理",
                meta: {
                  requiresAuth: false}, 
                component: () => import('@/views/detection/equip/index.vue'),
              } 
            ]
          },{
            path: '/detection/upkeep/site',
            name: 'site',
            title: "运行区域",
            icon: 'icon-shouye3',
            component: layout,
            redirect: '/detection/upkeep/site/sitendex',
            isTre: true,
            meta: { requiresAuth: false},
            children: [
              {
                path: 'sitendex',
                name: 'sitendex',
                title: "运行区域",
                meta: {
                  requiresAuth: false}, 
                component: () => import('@/views/detection/site/index.vue'),
              } 
            ]
          },{
            path: '/detection/upkeep/project',
            name: 'project',
            title: "项目管理",
            icon: 'icon-shouye3',
            component: layout,
            redirect: '/detection/upkeep/project/projectindex',
            isTre: true,
            meta: { requiresAuth: false},
            children: [
              {
                path: 'projectindex',
                name: 'projectindex',
                title: "项目管理",
                meta: {
                  requiresAuth: false}, 
                component: () => import('@/views/detection/project/index.vue'),
              } 
            ]
          },{
            path: '/detection/reason',
            name: 'reason',
            title: "开关机原因",
            icon: 'icon-shouye3',
            component: layout,
            redirect: '/detection/reason/reasonindex',
            isTre: true,
            meta: { requiresAuth: false},
            children: [
              {
                path: 'reasonindex',
                name: 'reasonindex',
                title: "开关机原因",
                meta: {
                  requiresAuth: false}, 
                 component: () => import('@/views/detection/reason/index.vue'),
              } 
            ]
          },{
            path: '/detection/point',
            name: 'point',
            title: "拉运处置点",
            icon: 'icon-shouye3',
            component: layout,
            redirect: '/detection/point/pointindex',
            isTre: true,
            meta: { requiresAuth: false},
            children: [
              {
                path: 'pointindex',
                name: 'pointindex',
                title: "拉运处置点",
                meta: {
                  requiresAuth: false}, 
                 component: () => import('@/views/detection/point/index.vue'),
              } 
            ]
          },{
            path: '/detection/temp',
            name: 'temp',
            title: "模板管理",
            icon: 'icon-shouye3',
            component: layout,
            redirect: '/detection/temp/tempindex',
            isTre: true,
            meta: { requiresAuth: false},
            children: [
              {
                path: 'tempindex',
                name: 'tempindex',
                title: "模板管理",
                meta: {
                  requiresAuth: false}, 
                component: () => import('@/views/temp/index.vue'),
              } 
            ]
          },{
            path: '/detection/system/company',
            name: 'company',
            title: "APP用户管理",
            icon: 'icon-shouye3',
            component: layout,
            redirect: '/detection/system/company/companyIndex',
            isTre: true,
            meta: { requiresAuth: false},
            children: [
              {
                path: 'companyIndex',
                name: 'companyIndex',
                title: "单位列表", 
                component: () => import('@/views/system/company/index.vue'),
              } 
            ]
          }],
      }, {
        path: '/detectio/car',
        name: 'car',
        title: "车辆管理",
        icon: 'icon-shouye3',
        component: layout,
        meta: { requiresAuth: false },
        

      }, {
        path: '/system',
        name: 'system',
        title: "系统管理",
        icon: 'icon-shouye3',
        component: layout,
        isTre: true,
    
        meta: { requiresAuth: false },
        children: [
          {
            path: '/detection/system/company',
            name: 'company',
            title: "单位管理",
            icon: 'icon-shouye3',
            component: layout,
            redirect: '/detection/system/company/companyIndex',
            isTre: true,
            meta: { requiresAuth: false},
            children: [
              {
                path: 'companyIndex',
                name: 'companyIndex',
                title: "单位列表",
                icon: 'icon-shouye3',
                permission: ['index', 'create'],
                component: () => import('@/views/system/company/index.vue'),
                meta: {
                  noCache: true,
                  requiresAuth: false,
                  isPenultimate: true,
                  button:[{title:'列表',type:1,value:'companyIndex:index'},{title:'创建',type:1,value:'companyIndex:create'},{title:'修改',type:1,value:'companyIndex:update'},{title:'删除',type:1,value:'companyIndex:delete'},{title:'状态修改',type:1,value:'companyIndex:status'}],
                  api:[{title:'列表',type:2,value:'company:index'},{title:'创建',type:2,value:'company:create'},{title:'修改',type:2,value:'company:update'},{title:'删除',type:2,value:'company:delete'},{title:'状态修改',type:2,value:'company:status'}]
                  
                },
              }
            ]
          }, {
            path: '/detection/system/admin',
            name: 'admin',
            title: "用户维护",
            icon: 'icon-shouye3',
            component: layout,
            redirect: '/detection/system/admin/adminIndex',
            meta: { requiresAuth: false },
            children: [
              {
                path: 'adminIndex',
                name: 'adminIndex',
                title: "用户列表",
                icon: 'icon-shouye3',
                permission: ['index', 'create'],
                component: () => import('@/views/system/admin/index.vue'),
                meta: {
                  requiresAuth: false,isPenultimate: true,
                  button:[{title:'列表',type:1,value:'adminIndex:index'},{title:'创建',type:1,value:'adminIndex:create'},{title:'修改',type:1,value:'adminIndex:update'},{title:'删除',type:1,value:'adminIndex:delete'},{title:'状态修改',type:1,value:'adminIndex:status'}],
                  api:[{title:'列表',type:2,value:'user:index'},{title:'创建',type:2,value:'user:create'},{title:'修改',type:2,value:'user:update'},{title:'删除',type:2,value:'user:delete'},{title:'状态修改',type:2,value:'user:status'}]
               
                },
              }
            ]
          }, {
            path: '/detection/system/role',
            name: 'role',
            title: "角色维护",
            icon: 'icon-shouye3',
            component: layout,
            redirect: '/detection/system/role/roleIndex',
            meta: { requiresAuth: false },
            children: [
              {
                path: 'roleIndex',
                name: 'roleIndex',
                title: "用户列表",
                icon: 'icon-shouye3',
                component: () => import('@/views/system/role/index.vue'),
                meta: { requiresAuth: false,isPenultimate: true,
                  button:[{title:'列表',type:1,value:'roleIndex:index'},{title:'创建',type:1,value:'roleIndex:create'},{title:'修改',type:1,value:'roleIndex:update'},{title:'删除',type:1,value:'roleIndex:delete'},{title:'状态修改',type:1,value:'roleIndex:status'}],
                  api:[{title:'列表',type:2,value:'role:index'},{title:'创建', type:2, value:'role:create'},{title:'修改',type:2,value:'role:update'},{title:'删除',type:2,value:'role:delete'},{title:'状态修改',type:2,value:'role:status'}]
               
                 },
              }
            ]
          }, {
            path: '/detection/system/menu',
            name: 'menu',
            title: "菜单维护",
            icon: 'icon-shouye3',
            component: menu,
            meta: { requiresAuth: false },
          }
        ]
      }
    ]
  }, {
    path: '/operation',
    name: 'operation',
    title:'数据检测',
    component: operation,

  }, {
    path: '/statistics',
    name: 'statistics',
    title:'数据大坪',

    component: statistics,
    children: [
      {
        path: '/statistics/analysis',
        name: 'analysis',
        title: "数据分析", 
      } 
    ]
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

// 全局前置守卫
router.beforeEach((to, from) => {
  // 而不是去检查每条路由记录
  console.log(to.meta.requiresAuth, '未登录')

  // to.matched.some(record => record.meta.requiresAuth)
  if (to.meta.requiresAuth) {
    // 此路由需要授权，请检查是否已登录
    // 如果没有，则重定向到登录页面
    console.log(to, '未登录')
    return {
      path: '/login',
      // 保存我们所在的位置，以便以后再来
      query: { redirect: to.fullPath },
    }
  }
})

export default router;